﻿#region Copyright (c) 2012-10, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Collections.Generic;


namespace Amarok
{
	/// <summary>
	/// This type provides extension methods for <see cref="IDictionary{TKey, TValue}"/>.
	/// </summary>
	public static class DictionaryExtensions
	{
		#region ++ Public Interface (AddOrUpdate) ++

		/// <summary>
		/// Adds a key/value pair to the dictionary if the key does not already exist, or updates a key/value 
		/// pair in the dictionary if the key already exists.
		/// </summary>
		/// 
		/// <typeparam name="TKey">
		/// The type of the dictionary key.</typeparam>
		/// <typeparam name="TValue">
		/// The type of the dictionary value.</typeparam>
		/// 
		/// <param name="dictionary">
		/// The dictionary collection.</param>
		/// <param name="key">
		/// The key to be added or whose value should be updated.</param>
		/// <param name="addValue">
		/// The value to be added for an absent key.</param>
		/// <param name="updateValueFunc">
		/// The function used to generate a new value for an existing key based on the key's existing value.</param>
		/// 
		/// <returns>
		/// The new value for the key. This will be either the supplied value (if the key was absent) or the 
		/// result of the update value function (if the key was present).</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static TValue AddOrUpdate<TKey, TValue>(
			this IDictionary<TKey, TValue> dictionary,
			TKey key,
			TValue addValue,
			Func<TKey, TValue, TValue> updateValueFunc)
		{
			Verify.NotNull(updateValueFunc, "updateValueFunc");

			TValue currentValue;

			if (dictionary.TryGetValue(key, out currentValue))
			{
				// update
				currentValue = updateValueFunc(key, currentValue);
				dictionary[key] = currentValue;
				return currentValue;
			}
			else
			{
				// add
				dictionary.Add(key, addValue);
				return addValue;
			}
		}

		/// <summary>
		/// Adds a key/value pair to the dictionary if the key does not already exist, or updates a key/value 
		/// pair in the dictionary if the key already exists.
		/// </summary>
		/// 
		/// <typeparam name="TKey">
		/// The type of the dictionary key.</typeparam>
		/// <typeparam name="TValue">
		/// The type of the dictionary value.</typeparam>
		/// 
		/// <param name="dictionary">
		/// The dictionary collection.</param>
		/// <param name="key">
		/// The key to be added or whose value should be updated.</param>
		/// <param name="addValueFunc">
		/// The function used to generate a value for an absent key.</param>
		/// <param name="updateValueFunc">
		/// The function used to generate a new value for an existing key based on the key's existing value.</param>
		/// 
		/// <returns>
		/// The new value for the key. This will be either the result of the supplied add value function (if the 
		/// key was absent) or the result of the supplied update value function (if the key was present).</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static TValue AddOrUpdate<TKey, TValue>(
			this IDictionary<TKey, TValue> dictionary,
			TKey key,
			Func<TKey, TValue> addValueFunc,
			Func<TKey, TValue, TValue> updateValueFunc)
		{
			Verify.NotNull(addValueFunc, "addValueFunc");
			Verify.NotNull(updateValueFunc, "updateValueFunc");

			TValue currentValue;

			if (dictionary.TryGetValue(key, out currentValue))
			{
				// update
				currentValue = updateValueFunc(key, currentValue);
				dictionary[key] = currentValue;
				return currentValue;
			}
			else
			{
				// add
				var value = addValueFunc(key);
				dictionary.Add(key, value);
				return value;
			}
		}

		#endregion

		#region ++ Public Interface (GetOrAdd) ++

		/// <summary>
		/// Adds a key/value pair to the dictionary if the key does not already exist.
		/// </summary>
		/// 
		/// <typeparam name="TKey">
		/// The type of the dictionary key.</typeparam>
		/// <typeparam name="TValue">
		/// The type of the dictionary value.</typeparam>
		/// 
		/// <param name="dictionary">
		/// The dictionary collection.</param>
		/// <param name="key">
		/// The key of the dictionary entry, whose value should be returned.</param>
		/// <param name="value">
		/// The value to be added, if the key does not already exist.</param>
		/// 
		/// <returns>
		/// The value for the key. This will be either the existing value for the key if the key is already 
		/// in the dictionary, or the new value if the key was not in the dictionary.</returns>
		public static TValue GetOrAdd<TKey, TValue>(
			this IDictionary<TKey, TValue> dictionary,
			TKey key,
			TValue value)
		{
			TValue currentValue;

			if (dictionary.TryGetValue(key, out currentValue))
			{
				// get
				return currentValue;
			}
			else
			{
				// add
				dictionary.Add(key, value);
				return value;
			}
		}

		/// <summary>
		/// Adds a key/value pair to the dictionary if the key does not already exist.
		/// </summary>
		/// 
		/// <typeparam name="TKey">
		/// The type of the dictionary key.</typeparam>
		/// <typeparam name="TValue">
		/// The type of the dictionary value.</typeparam>
		/// 
		/// <param name="dictionary">
		/// The dictionary collection.</param>
		/// <param name="key">
		/// The key of the dictionary entry, whose value should be returned.</param>
		/// <param name="valueFunc">
		/// The function used to generate a value for the key.</param>
		/// 
		/// <returns>
		/// The value for the key. This will be either the existing value for the key if the key is already 
		/// in the dictionary, or the new value for the key as returned by the supplied value function if the 
		/// key was not in the dictionary.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static TValue GetOrAdd<TKey, TValue>(
			this IDictionary<TKey, TValue> dictionary,
			TKey key,
			Func<TKey, TValue> valueFunc)
		{
			Verify.NotNull(valueFunc, "valueFunc");

			TValue currentValue;

			if (dictionary.TryGetValue(key, out currentValue))
			{
				// get
				return currentValue;
			}
			else
			{
				// add
				var value = valueFunc(key);
				dictionary.Add(key, value);
				return value;
			}
		}

		#endregion

		#region ++ Public Interface (GetValueOrDefault) ++

		/// <summary>
		/// Returns the value of the dictionary entry with the specified key, if such an entry exists. 
		/// Otherwise the default value (null or zero) of the value's type is returned.
		/// </summary>
		/// 
		/// <typeparam name="TKey">
		/// The type of the dictionary key.</typeparam>
		/// <typeparam name="TValue">
		/// The type of the dictionary value.</typeparam>
		/// 
		/// <param name="dictionary">
		/// The dictionary collection.</param>
		/// <param name="key">
		/// The key of the dictionary entry, whose value should be returned.</param>
		/// 
		/// <returns>
		/// The dictionary entry's value if an entry with the specified key exists; otherwise the default value 
		/// of the value's type.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static TValue GetValueOrDefault<TKey, TValue>(
			this IDictionary<TKey, TValue> dictionary,
			TKey key)
		{
			TValue value;
			dictionary.TryGetValue(key, out value);
			return value;
		}

		/// <summary>
		/// Returns the value of the dictionary entry with the specified key, if such an entry exists. 
		/// Otherwise the supplied default value is returned.
		/// </summary>
		/// 
		/// <typeparam name="TKey">
		/// The type of the dictionary key.</typeparam>
		/// <typeparam name="TValue">
		/// The type of the dictionary value.</typeparam>
		/// 
		/// <param name="dictionary">
		/// The dictionary collection.</param>
		/// <param name="key">
		/// The key of the dictionary entry, whose value should be returned.</param>
		/// <param name="defaultValue">
		/// The default value to return when no dictionary entry with the specified key can be found.</param>
		/// 
		/// <returns>
		/// The dictionary entry's value if an entry with the specified key exists; otherwise the supplied
		/// default value.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static TValue GetValueOrDefault<TKey, TValue>(
			this IDictionary<TKey, TValue> dictionary,
			TKey key,
			TValue defaultValue)
		{
			TValue value;

			if (dictionary.TryGetValue(key, out value))
				return value;

			return defaultValue;
		}

		#endregion

	}
}
